using System;
using System.Data;
using System.Data.OleDb;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;

using PCSComUtils.PCSExc;

namespace PCSComPayroll.DS
{
	public class PAY_ProductionNormDS //: IObjectDS
	{
		public PAY_ProductionNormDS()
		{
		}

		private const string THIS = "PCSComPayroll.DS.DS.PAY_ProductionNormDS";
		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";

			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				PAY_ProductionNormVO objObject = (PAY_ProductionNormVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);

				strSql = "INSERT INTO PAY_ProductionNorm("
					+ PAY_ProductionNormTable.PRODUCTID_FLD + ","
					+ PAY_ProductionNormTable.TOTALTIME_FLD + ","
					+ PAY_ProductionNormTable.FROMDATE_FLD + ","
					+ PAY_ProductionNormTable.TODATE_FLD + ")"
					+ "VALUES(?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_ProductionNormTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.TOTALTIME_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_ProductionNormTable.TOTALTIME_FLD].Value = objObject.TotalTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.FROMDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_ProductionNormTable.FROMDATE_FLD].Value = objObject.FromDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.TODATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_ProductionNormTable.TODATE_FLD].Value = objObject.ToDate;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql = "DELETE " + PAY_ProductionNormTable.TABLE_NAME + " WHERE  " + "ProductionNormID" + "=" + pintID.ToString();
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}


			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Delete(string pstrIDList)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "DELETE " + PAY_ProductionNormTable.TABLE_NAME
					+ " WHERE " + PAY_ProductionNormTable.SALEORDERDETAILID_FLD + " IN (" + pstrIDList + ")";
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}


			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}
		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();

			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				strSql = "SELECT "
					+ PAY_ProductionNormTable.PRODUCTIONNORMID_FLD + ","
					+ PAY_ProductionNormTable.PRODUCTID_FLD + ","
					+ PAY_ProductionNormTable.TOTALTIME_FLD + ","
					+ PAY_ProductionNormTable.FROMDATE_FLD + ","
					+ PAY_ProductionNormTable.TODATE_FLD
					+ " FROM " + PAY_ProductionNormTable.TABLE_NAME
					+ " WHERE " + PAY_ProductionNormTable.PRODUCTIONNORMID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				PAY_ProductionNormVO objObject = new PAY_ProductionNormVO();

				while (odrPCS.Read())
				{
					objObject.ProductionNormID = int.Parse(odrPCS[PAY_ProductionNormTable.PRODUCTIONNORMID_FLD].ToString());
					objObject.ProductID = int.Parse(odrPCS[PAY_ProductionNormTable.PRODUCTID_FLD].ToString());
					try
					{
						objObject.TotalTime = decimal.Parse(odrPCS[PAY_ProductionNormTable.TOTALTIME_FLD].ToString());
					}
					catch
					{
						objObject.TotalTime = 0;
					}
					try
					{
						objObject.FromDate = DateTime.Parse(odrPCS[PAY_ProductionNormTable.FROMDATE_FLD].ToString());
					}
					catch{}
					try
					{
						objObject.ToDate = DateTime.Parse(odrPCS[PAY_ProductionNormTable.TODATE_FLD].ToString());
					}
					catch{}

				}
				return objObject;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			PAY_ProductionNormVO objObject = (PAY_ProductionNormVO) pobjObjecVO;


			//prepare value for parameters
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql = "UPDATE PAY_ProductionNorm SET "
					+ PAY_ProductionNormTable.PRODUCTID_FLD + "=   ?" + ","
					+ PAY_ProductionNormTable.TOTALTIME_FLD + "=   ?" + ","
					+ PAY_ProductionNormTable.FROMDATE_FLD + "=   ?" + ","
					+ PAY_ProductionNormTable.TODATE_FLD + "=  ?"
					+ " WHERE " + PAY_ProductionNormTable.PRODUCTIONNORMID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_ProductionNormTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.TOTALTIME_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_ProductionNormTable.TOTALTIME_FLD].Value = objObject.TotalTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.FROMDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_ProductionNormTable.FROMDATE_FLD].Value = objObject.FromDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.TODATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_ProductionNormTable.TODATE_FLD].Value = objObject.ToDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_ProductionNormTable.PRODUCTIONNORMID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_ProductionNormTable.PRODUCTIONNORMID_FLD].Value = objObject.ProductionNormID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();


			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;

				strSql = "SELECT "
					+ PAY_ProductionNormTable.PRODUCTIONNORMID_FLD + ","
					+ PAY_ProductionNormTable.PRODUCTID_FLD + ","
					+ PAY_ProductionNormTable.TOTALTIME_FLD + ","
					+ PAY_ProductionNormTable.FROMDATE_FLD + ","
					+ PAY_ProductionNormTable.TODATE_FLD
					+ " FROM " + PAY_ProductionNormTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_ProductionNormTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS = null;
			OleDbCommandBuilder odcbPCS;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql = "SELECT "
					+ PAY_ProductionNormTable.PRODUCTIONNORMID_FLD + ","
					+ PAY_ProductionNormTable.PRODUCTIONLINEID_FLD + ","
					+ PAY_ProductionNormTable.TOTALTIME_FLD + ","
					+ PAY_ProductionNormTable.SALEORDERDETAILID_FLD + ","
					+ PAY_ProductionNormTable.USERID_FLD
					+ " FROM " + PAY_ProductionNormTable.TABLE_NAME;


				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData, PAY_ProductionNormTable.TABLE_NAME);

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}
		/// <summary>
		/// Get total leadtime group by Production Line
		/// </summary>
		/// <returns></returns>
		public DataTable GetRunTime(int pintSaleOrderDetailID)
		{
			const string METHOD_NAME = THIS + ".GetRunTime()";
			
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "SELECT D.ProductionLineID, SUM(ISNULL(LaborRunTime,0) * D.OrderQuantity) LaborRunTime"
					+ " FROM PRO_WORouting R JOIN PRO_WorkOrderDetail D ON R.WorkOrderDetailID = D.WorkOrderDetailID"
					+ " JOIN PRO_WorkOrderMaster M ON M.WorkOrderMasterID = R.WorkOrderMasterID"
					+ " WHERE ISNULL(CalculateTime,0) = 1"
					+ " AND ISNULL(D.ProductionLineID,0) > 0"
					+ " AND D.SaleOrderDetailID = " + pintSaleOrderDetailID
					+ " GROUP BY D.ProductionLineID";
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				DataTable dtbDataTable = new DataTable(PRO_WORoutingTable.TABLE_NAME);
				odadPCS.Fill(dtbDataTable);

				return dtbDataTable;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

	}
}